<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/
TR/html4/strict.dtd">
<HTML>
<HEAD>
	<TITLE>Inzendopdracht 051R3</TITLE>
</HEAD>
<BODY>
	<FORM ACTION="051R3.php" METHOD="POST">
	<H2>Bingokaart waarop BINGO is gevallen</H2>
	<?php
	DEFINE("MIN_NUMBER", 10);
	DEFINE("MAX_NUMBER", 69);
	$cardNumbers = fillCard();
	$pulledNumbers = array();	

	//pull number until bingo
	do {
		$number = pullNumber();
	} while (!bingo($number));
	
	//Display bingo card
	echo printCard($cardNumbers);
	//Display pulled numbers
	echo 'Getrokken getallen: <table border="1" cellpadding="4"><TR><TD>' . 
		implode("</TD><TD>", $pulledNumbers) . "</table></TR></TD><BR>";
	//Display total numbers count	
	echo "Aantal getallen dat is getrokken: " . count($pulledNumbers) . "<BR><BR>";

	//Fill array with random numbers to display on card
	function fillCard()	{
		$usedNUmbers = array();
		for ($i = 0; $i <= 5; $i++)	{
			for ($j = 0; $j <= 5; $j++) {
				//Possible numbers depends on rownumber
				$rowMin = ($i + 1) * 10;
				$rowMax = $rowMin + 9;
				$number = rand($rowMin, $rowMax);
				while (in_array($number, $usedNUmbers)) {
					$number = rand($rowMin, $rowMax);
				}
				$usedNUmbers[] = $number;
				$cardNumbers[$i][$j] = $number;
			}
			//last row and last column are used to test for bingo.
			$cardNumbers[$i][6] = 0;
			$cardNumbers[6][$i] = 0;
		}
		return $cardNumbers;
	}

	//Display card with random numbers
	function printCard($cardNumbers) {
		global $pulledNumbers;
		$html = '<P><table border="1" cellpadding="4">';
		for ($i = 0; $i <= 5; $i++)	{
			for ($j = 0; $j <= 5; $j++) {
				if ($cardNumbers[$i][6] == 6 OR $cardNumbers[6][$j] == 6) {
					$td = '<TD bgcolor="green">';
				} else {
					$td = '<TD>';
				}
				$html .= $td . $cardNumbers[$i][$j] . "</TD>";
			}
			$html .= "</TR>";
		}
		$html .= "</TABLE></P>";
		return $html;
	}
	
	//Pull random number between MIN_NUMBER and MAX_NUMBER
	//keep track of used numbers
	function pullNumber() {
		global $pulledNumbers;
		do {
			$number = rand(MIN_NUMBER, MAX_NUMBER);
		} while (in_array($number, $pulledNumbers));
		$pulledNumbers[] = $number;
		return $number;
	}

	//check for bingo. Returns true if bingo
	function bingo($number) {
		global $cardNumbers;
		$bingo = false;
		for ($rowNum = 0; $rowNum <= 5; $rowNum++) {
			$colNum = array_search($number, $cardNumbers[$rowNum]);
			if ($colNum !== false) {
				$cardNumbers[$rowNum][6] += 1;
				$cardNumbers[6][$colNum] += 1;
				if ($cardNumbers[$rowNum][6] == 6 OR $cardNumbers[6][$colNum] == 6) {
					$bingo = true;
				}
			}
		}
		return $bingo;
	}
	?>
	<INPUT TYPE="submit" NAME="newgame" VALUE="Speel opnieuw">
</BODY>
</HTML>